(function(){
//JS 初始化页面大小 以高度为百分百。自适应宽度，按ip6比例锁定0.56
var dsn=$(window).height()/$(window).width();
//$('.arealayer').width($(window).height()*0.56);
$('.arealayer').width($(window).width());
$('.arealayer').height($(window).height()); 
 
//配置
var config = {
	'audio':{
		'icon':'audio-record-play',
		'text':true
	},
	'loading': 'loading-ic'
};

//loading
window.onload = function(){
	$('#loading').hide();
}

//END页面和分享框
$('.js-btn-share,.js-btn-showshare,.shareshutdiv').bind('tap',function(){
	$('.share').show();
})
$('.js-btn-share,.js-btn-showpaihang').bind('tap',function(){
	
	getajax('no');
	$('.paihang,.paihang .shutdiv').show();
	
})
$('.js-btn-reload').bind('tap',function(){
	   window.location.reload();

})
$('.share').bind('tap',function(){
	$(this).hide();
});


//开始页面切换流程
var pageIndex = 1,
	pageTotal = $('.page').length,
	towards = { up:1, right:2, down:3, left:4},
	isAnimating = false;

//禁用手机默认的触屏滚动行为
document.addEventListener('touchmove',function(event){
	event.preventDefault(); },false);

//页面从右向左走
$(document).swipeLeft(function(){
	if (isAnimating) return;
	if (pageIndex < pageTotal) { 
		pageIndex+=1; 
	pageMove(towards.up);
	$('.share').hide();	
	}else{
		//pageIndex=1;
	}; 
	
})

//页面从左向向走
$(document).swipeRight(function(){
	if (isAnimating) return;
	if (pageIndex > 1) { 
		pageIndex-=1; 
	pageMove(towards.down);	
	$('.share').hide();
	}else{
		//pageIndex=pageTotal;
	};
})

//页面切换主流程
function pageMove(tw){
	var lastPage;
	if(tw=='1'){
		if(pageIndex==1){
			lastPage = ".page-"+pageTotal;
		}else{
			lastPage = ".page-"+(pageIndex-1);
		}
		
	}else if(tw=='3'){
		if(pageIndex==pageTotal){
			lastPage = ".page-1";
		}else{
			lastPage = ".page-"+(pageIndex+1);
		}
		
	}

	var nowPage = ".page-"+pageIndex;
	
	switch(tw) {
		case towards.up:
			outClass = 'pt-page-moveToLeft';
			inClass = 'pt-page-moveFromRight';
			break;
		case towards.down:
			outClass = 'pt-page-moveToRight';
			inClass = 'pt-page-moveFromLeft';
			break;
	}
	isAnimating = true;
	$(nowPage).removeClass("hide");
	
	$(lastPage).addClass(outClass);
	$(nowPage).addClass(inClass);
		$('.maptip').removeClass('animation-maptip');
		$('.js').removeClass('animation-js');
	
	setTimeout(function(){
		$(lastPage).removeClass('page-current');
		$(lastPage).removeClass(outClass);
		$(lastPage).addClass("hide");
		$(lastPage).find("img").addClass("hide");
		
		$(nowPage).addClass('page-current');
		$(nowPage).removeClass(inClass);
		$(nowPage).find("img").removeClass("hide");
		
		$(nowPage+' .maptip.t1').addClass('animation-maptip');
		$(nowPage+' .js.js1').addClass('animation-js');
		$(nowPage+' .a-taps .c1').addClass('animation-scale');
		
		isAnimating = false;
	},600);
	
	setTimeout(function(){
		$(nowPage+' .maptip.t2').addClass('animation-maptip');
		$(nowPage+' .js.js2').addClass('animation-js');
		$(nowPage+' .a-taps .c2').addClass('animation-scale');
	},800);
	setTimeout(function(){
		$(nowPage+' .maptip.t3').addClass('animation-maptip');
		$(nowPage+' .js.js3').addClass('animation-js');
		$(nowPage+' .a-taps .c3').addClass('animation-scale');
	},900);
	setTimeout(function(){
		$(nowPage+' .maptip.t4').addClass('animation-maptip');
		$(nowPage+' .a-taps .c4').addClass('animation-scale');
	},1300);
	setTimeout(function(){
		$(nowPage+' .maptip.t5').addClass('animation-maptip');
		$(nowPage+' .a-taps .c5').addClass('animation-scale');
	},1500);
	setTimeout(function(){
		$(nowPage+' .maptip.t6').addClass('animation-maptip');
		$(nowPage+' .a-taps .c6').addClass('animation-scale');
	},1800);
	setTimeout(function(){
		$(nowPage+' .maptip.t7').addClass('animation-maptip');
		$(nowPage+' .a-taps .c7').addClass('animation-scale');
	},2000);
}

//页面打开三秒后开始加载全部大体积图片，加块显示速度。经实验。非3GWIFI环境会更卡，临时屏蔽
setTimeout(function(){
	var html='';
	/*
	html+='<img src="img/hb/n1t.png" width="100">';
	html+='<img src="img/hb/n1bg.jpg" width="100">';
	html+='<img src="img/hb/n2t.png" width="100">';
	html+='<img src="img/hb/n2bg.jpg" width="100">';
	html+='<img src="img/hb/n3t.png" width="100">';
	html+='<img src="img/hb/n3bg.jpg" width="100">';
	html+='<img src="img/hb/n4t.png" width="100">';
	html+='<img src="img/hb/n4bg.jpg" width="100">';
	html+='<img src="img/hb/n5t.png" width="100">';
	html+='<img src="img/hb/n5bg.jpg" width="100">';
	
	html+='<img src="img/db/n1t.png" width="100">';
	html+='<img src="img/db/n1bg.jpg" width="100">';
	html+='<img src="img/db/n2t.png" width="100">';
	html+='<img src="img/db/n2bg.jpg" width="100">';
	html+='<img src="img/db/n3t.png" width="100">';
	html+='<img src="img/db/n3bg.jpg" width="100">';
	
	html+='<img src="img/hd/n1t.png" width="100">';
	html+='<img src="img/hd/n1bg.jpg" width="100">';
	html+='<img src="img/hd/n2t.png" width="100">';
	html+='<img src="img/hd/n2bg.jpg" width="100">';
	html+='<img src="img/hd/n3t.png" width="100">';
	html+='<img src="img/hd/n3bg.jpg" width="100">';
	html+='<img src="img/hd/n4t.png" width="100">';
	html+='<img src="img/hd/n4bg.jpg" width="100">';
	html+='<img src="img/hd/n5t.png" width="100">';
	html+='<img src="img/hd/n5bg.jpg" width="100">';
	html+='<img src="img/hd/n6t.png" width="100">';
	html+='<img src="img/hd/n6bg.jpg" width="100">';
	html+='<img src="img/hd/n7t.png" width="100">';
	html+='<img src="img/hd/n7bg.jpg" width="100">';
	
	
	html+='<img src="img/hz/n1t.png" width="100">';
	html+='<img src="img/hz/n1bg.jpg" width="100">';
	html+='<img src="img/hz/n2t.png" width="100">';
	html+='<img src="img/hz/n2bg.jpg" width="100">';
	html+='<img src="img/hz/n3t.png" width="100">';
	html+='<img src="img/hz/n3bg.jpg" width="100">';
	
	html+='<img src="img/hn/n1t.png" width="100">';
	html+='<img src="img/hn/n1bg.jpg" width="100">';
	html+='<img src="img/hn/n2t.png" width="100">';
	html+='<img src="img/hn/n2bg.jpg" width="100">';
	html+='<img src="img/hn/n3t.png" width="100">';
	html+='<img src="img/hn/n3bg.jpg" width="100">';
	
	
	html+='<img src="img/xn/n1t.png" width="100">';
	html+='<img src="img/xn/n1bg.jpg" width="100">';
	html+='<img src="img/xn/n2t.png" width="100">';
	html+='<img src="img/xn/n2bg.jpg" width="100">';
	html+='<img src="img/xn/n3t.png" width="100">';
	html+='<img src="img/xn/n3bg.jpg" width="100">';
	html+='<img src="img/xn/n4t.png" width="100">';
	html+='<img src="img/xn/n4bg.jpg" width="100">';
	html+='<img src="img/xn/n5t.png" width="100">';
	html+='<img src="img/xn/n5bg.jpg" width="100">';
	
	
	html+='<img src="img/xb/n1t.png" width="100">';
	html+='<img src="img/xb/n1bg.jpg" width="100">';
	html+='<img src="img/xb/n2t.png" width="100">';
	html+='<img src="img/xb/n2bg.jpg" width="100">';
	html+='<img src="img/xb/n3t.png" width="100">';
	html+='<img src="img/xb/n3bg.jpg" width="100">';
	html+='<img src="img/xb/n4t.png" width="100">';
	html+='<img src="img/xb/n4bg.jpg" width="100">';
	html+='<img src="img/xb/n5t.png" width="100">';
	html+='<img src="img/xb/n5bg.jpg" width="100">';
	
	html+='<img src="img/ga/n1t.png" width="100">';
	html+='<img src="img/ga/n1bg.jpg" width="100">';
	html+='<img src="img/ga/n2t.png" width="100">';
	html+='<img src="img/ga/n2bg.jpg" width="100">';*/
	
		$(' .hidecontent').html(html);
},6000);
	
	
//定义各城市按钮
$('.a-taps li,.maptip').tap(function () {
    var area,prov;
	area=$(this).parents('.page').attr('data-list');
	prov=$(this).attr('data-list');
	$(".floatlayer img.prov").attr('src','img/'+area+'/n'+prov+'t.png');
	$(".floatlayer img.countimg").attr('data-list',area+prov);
	$(".floatlayerbg").css('background-image','url(img/'+area+'/n'+prov+'bg.jpg)');
	$(".floatlayerbg,.floatlayer,.shutdiv ").show();	
});

//定义shut按钮
$('.shutdiv').tap(function () {
	$(".floatlayer img.prov").attr('src','img/blank.png');
	$(".floatlayer img.countimg").attr('data-list','0');
	$(".floatlayerbg").css('background-image','url(img/blank.png)');
	$(".floatlayer").css('top','0');
	$(".floatlayerbg,.floatlayer,.shutdiv,.paihang ").hide();		
	$(".floatlayer img.countimg").attr('src','img/qd.png');
});

//定义签到提交
$('.countimg').tap(function () {
	
getajax($(this).attr('data-list'));
	
	$(".floatlayer img.countimg").attr('src','img/qd2.png');
	
	//$(".paihang").show();		
	
	
});

//定义startpage按钮
$('.startpage').tap(function () { 
	$(".startpages").hide();		
});


/*定义滑动层。可手动拖动层*/
  var moveX,moveY,startX,startY;
  $(document).on("touchstart","#floatlayer",function(event){
      var touchPros = event.originalEvent.touches[0];
      //startX = touchPros.pageX - event.target.parentNode.offsetLeft;
      startY = touchPros.pageY - event.target.parentNode.offsetTop;
    return false;
  }).on("touchmove","#floatlayer",function(event){
      var touchPros = event.originalEvent.touches[0];
      //moveX = touchPros.pageX - startX;
      moveY = touchPros.pageY - startY;
      //$('#floatlayer').css('left',moveX).css('top',moveY);
      //当下拉到元素顶部时禁止继续拉动
	  if (moveY >0){ moveY = 0}
      //当上拉到元素底部时禁止继续拉动
	  if ($(window).height()-moveY > $('#floatlayer').height()){moveY=$(window).height()-$('#floatlayer').height()}
      $('#floatlayer').css('top',moveY);
  });

})();


//提交签到数据并返回排行
function getajax(id){
		
$.ajax({
  type: 'GET',
  url: 'http://table.chinanews.com/txtcount/countjson.php',
  // data to be added to query string:
  data: { id: '150301',to: id},
  // type of data we are expecting in return:
  dataType: 'jsonp',
  jsonp: "callback",
  jsonpCallback: "view",
  timeout: 300,
  context: $('body'),
  success: function(data){
    // Supposing this JSON payload was received:
    //   {"project": {"id": 42, "html": "<div>..." }}
    // append the HTML to context object.
	//console.log(data);
	//data = eval("(" + data + ")");

	//取得排行后，进行取TOP5的排序。然后插入到TOP5显示中
	var thisarray = new Array();
	var no1='';
	var no2='';
	var no4='';
	var no3='';
	var no5='';
	var nol1='';
	var nol2='';
	var nol4='';
	var nol3='';
	var nol5='';
	var no1t=0;
	var no2t=0;
	var no3t=0;
	var no4t=0;
	var no5t=0;
	var htmlist='';
	 for(var n in data[0]){ 
	 console.log(n+'|'+data[0][n]);
	 	//thisarray[n]=data[0][n];
		if (data[0][n]>no1t){
			nol5=nol4;no5=no4;no5t=no4t;
			nol4=nol3;no4=no3;no4t=no3t;
			nol3=nol2;no3=no2;no3t=no2t;
			nol2=nol1;no2=no1;no2t=no1t;
			no1t=data[0][n];
			no1 = n.substr(0, 2);
			nol1 = n.substr(2, 1);
		}else if (data[0][n]>no2t){			
			nol5=nol4;no5=no4;no5t=no4t;
			nol4=nol3;no4=no3;no4t=no3t;
			nol3=nol2;no3=no2;no3t=no2t;
			no2t=data[0][n];
			no2 = n.substr(0, 2);
			nol2 = n.substr(2, 1);
		}else if (data[0][n]>no3t){			
			nol5=nol4;no5=no4;no5t=no4t;
			nol4=nol3;no4=no3;no4t=no3t;
			no3t=data[0][n];
			no3 = n.substr(0, 2);
			nol3 = n.substr(2, 1);
		}else if (data[0][n]>no4t){			
			nol5=nol4;no5=no4;no5t=no4t;
			no4t=data[0][n];
			no4 = n.substr(0, 2);
			nol4 = n.substr(2, 1);
		}else if (data[0][n]>no5t){
			no5t=data[0][n];
			no5 = n.substr(0, 2);
			nol5 = n.substr(2, 1);
		}
		
	 console.log(no1t+'|'+no2t+'|'+no3t+'|'+no4t+'|'+no5t);
		
      } 
	  
	  
thisarray['hb1']="北京";
thisarray['hb2']="天津"
thisarray['hb3']="河北";
thisarray['hb4']="山西"
thisarray['hb5']="内蒙古";
thisarray['db1']="黑龙江"
thisarray['db2']="吉林"
thisarray['db3']="辽宁"
thisarray['hd1']="上海";
thisarray['hd2']="江苏"
thisarray['hd3']="浙江";
thisarray['hd4']="安徽"
thisarray['hd5']="福建";
thisarray['hd6']="江西"
thisarray['hd7']="山东";
thisarray['hz1']="河南"
thisarray['hz2']="湖北"
thisarray['hz3']="湖南"
thisarray['hn1']="广东"
thisarray['hn2']="广西"
thisarray['hn3']="海南"
thisarray['xn1']="重庆";
thisarray['xn2']="四川"
thisarray['xn3']="贵州";
thisarray['xn4']="云南"
thisarray['xn5']="西藏";
thisarray['xb1']="陕西";
thisarray['xb2']="甘肃"
thisarray['xb3']="青海";
thisarray['xb4']="宁夏"
thisarray['xb5']="新疆"
thisarray['ga1']="香港";
thisarray['ga2']="澳门";

//以top1为百分百显示条转换比例。插入到TOP5显示中
	   var percents=0;
	  $('.pailist .l1 .licon').css('background-image','url(img/'+no1+'/n'+nol1+'.png)');
	  $('.pailist .l1 .rcont .city span').html(thisarray[no1+nol1]);
	   $('.pailist .l1 .rcont .lie .num').html(no1t); 
	   $('.pailist .l1 .rcont .lie .pre').css('width','60%');
	   
	   
	  $('.pailist .l2 .licon').css('background-image','url(img/'+no2+'/n'+nol2+'.png)');
	  $('.pailist .l2 .rcont .city span').html(thisarray[no2+nol2]);
	   $('.pailist .l2 .rcont .lie .num').html(no2t); 
	   percents=no2t/no1t*60;
	   $('.pailist .l2 .rcont .lie .pre').css('width',percents+'%');
	   
	  $('.pailist .l3 .licon').css('background-image','url(img/'+no3+'/n'+nol3+'.png)');
	  $('.pailist .l3 .rcont .city span').html(thisarray[no3+nol3]);
	   $('.pailist .l3 .rcont .lie .num').html(no3t);  
	   percents=no3t/no1t*60;
	   $('.pailist .l3 .rcont .lie .pre').css('width',percents+'%');
	   
	  $('.pailist .l4 .licon').css('background-image','url(img/'+no4+'/n'+nol4+'.png)');
	  $('.pailist .l4 .rcont .city span').html(thisarray[no4+nol4]);
	   $('.pailist .l4 .rcont .lie .num').html(no4t);  
	   percents=no4t/no1t*60;
	   $('.pailist .l4 .rcont .lie .pre').css('width',percents+'%');
	   
	  $('.pailist .l5 .licon').css('background-image','url(img/'+no5+'/n'+nol5+'.png)');
	  $('.pailist .l5 .rcont .city span').html(thisarray[no5+nol5]);
	   $('.pailist .l5 .rcont .lie .num').html(no5t); 
	   percents=no5t/no1t*60;
	   $('.pailist .l5 .rcont .lie .pre').css('width',percents+'%'); 
	  
	 
  },
  error: function(xhr, type){
    //alert('Ajax error!')
  }
})  	
}